/**
 * @file
 *           base64.h
 * @brief
 *          <fillin>purpose
 *
 * @par Author (last changes):
 *          - Herea Adrian
 *          
 * @par Description:
 *
 *
 * @par Module-History:
 * @verbatim
 *  Date        Author				Reason
 *  15.01.2010  Herea Adrian         Initial creation
 *
 * @endverbatim
 *
 * @par Copyright Notice:
 * @verbatim
 * Copyright (C) 2010
 * All Rights Reserved.
 * The reproduction, transmission or use of this document or its contents is
 * not permitted without express written authority.
 * Offenders will be liable for damages. All rights, including rights created
 * by patent grant or registration of a utility model or design, are reserved.
 **/
#include "FileServer.h"

using namespace std;
FileServer::FileServer(void)
{
  cmd = new Command();
  m_FSOperations = FSOperations::getInstance();
  
  m_comandImp = new ComandsImpl();
  m_comandImp->setFsOperations(m_FSOperations);

  m_FSOperations->serializeMemoryMap();
}

FileServer::~FileServer(void)
{
	m_FSOperations->serializeMemoryMap();
}

int
FileServer::start(int port)
{
  HOSTENT* local_host;
  CHAR* local_ip;
  WSADATA wsaData;
  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
      exit(-1);
    }
  local_host = gethostbyname("");
  local_ip = inet_ntoa(*(struct in_addr*) *local_host->h_addr_list);
  string ipAddr(local_ip);
  m_sockServer = SocketServer::init(ipAddr, port);
  if (m_sockServer == NULL)
    {
      exit(-1);
    }
  m_sockServer->registerListener(this);
  m_sockServer->run();
  return 0;
}

void
FileServer::receive(string &protocol)
{
  cmd = Protocol::command(protocol);
  executeCommand();
}

void
FileServer::executeCommand()
{
	switch (cmd->getId())
	{
	case CMD_GET_SERVERS_ADRESSES:
		{
			m_comandImp->sendServersAddress( m_sockServer);
			printf("\nServer list requested\n");
		}
		break;
	case CMD_CHANGE_DIR:
		{
			m_comandImp->sendChangeDirResult( m_sockServer, (*cmd->getParams().begin()).getValue());
			printf("\nCMD_CHANGE_DIR  requested\n");
		}
		break;
	case CMD_MAKE_DIR:
		{
			printf("\n CMD_MAKE_DIR  requested \n");
			std::list<protocol::Param>::iterator it = cmd->getParams().begin();
			if(it != cmd->getParams().end() )
			{
				m_comandImp->sendCreateNewDir(m_sockServer, it->getValue());
			}
			else
			{
				//send error
				m_comandImp->sendError( m_sockServer,"CMD_MAKE_DIR");
			}
		}
		break;
	case CMD_LIST_DIR:
		{
			m_comandImp->sendDirContent(m_sockServer,(*cmd->getParams().begin()).getValue());
			printf("\nCMD_LIST_DIR  requested\n");
			
		}
		break;
	case CMD_SHOW_FILE:
		{
			m_comandImp->sendError( m_sockServer,"CMD_SHOW_FILE");
			printf("\nCMD_SHOW_FILE  requested\n");
			printf("\nCMD_ %d notimplemented\n", cmd->getId());
		}
		break;
	case CMD_NEW_FILE:
		{
			printf("\n CMD_NEW_FILE  requested \n");

			std::list<protocol::Param>::iterator it = cmd->getParams().begin();
			if(it != cmd->getParams().end() )
			{
				m_comandImp->sendCreateNewFile(m_sockServer, it->getValue());
			}
			else
			{
				//send error
				m_comandImp->sendError( m_sockServer,"CMD_NEW_FILE");
			}
		}
		break;
	case CMD_EXIT:
		{
			m_comandImp->sendError( m_sockServer,"CMD_EXIT");
			printf("\nCMD_EXIT  requested\n");
			printf("\nCMD_ %d notimplemented\n", cmd->getId());
		}
		break;
	case CMD_LIST_END:
		{
			m_comandImp->sendError( m_sockServer,"CMD_LIST_END");
			printf("\nCMD_MAKE_DIR  requested\n");
			printf("\nCMD_ %d notimplemented\n", cmd->getId());
		}
		break;
	case CMD_QUERY_AVAILABLE_SPACE:
		{
			printf("\n CMD_QUERY_AVAILABLE_SPACE  requested \n");
			m_comandImp->sendAvailableSpace(m_sockServer);
		}
		break;
	case CMD_PATH_EXISTS:
		{
			printf("\n CMD_PATH_EXISTS  requested \n");
			std::list<protocol::Param>::iterator it = cmd->getParams().begin();
			if(it != cmd->getParams().end() )
			{
				m_comandImp->sendPathExists(m_sockServer, it->getValue() );
			}
			else
			{
				//send error
				m_comandImp->sendError( m_sockServer,"CMD_PATH_EXISTS");
			}
		}
		break;
	default:
		{
			printf("\nCMD_ %d notimplemented\n", cmd->getId());
			//do nothing
		}
	}

}
